package com.sxbbc.seller.service;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.core.entity.jz.OrderDetail;
import com.sxbbc.common.core.entity.jz.OrderExpDetail;
import com.sxbbc.common.core.entity.jz.OrderMain;

/**
 * 数据导出 业务层
 *
 * @author leehao
 * @date 2019-05-07 17:34
 */
@Service
public class ExportService {

    /**
     * 导出订单列表
     * @param records 订单数据
     * @return HSSFWorkbook
     */
	@SuppressWarnings("deprecation")
	public HSSFWorkbook exportOrder(List<OrderMain> list) {
		 // 行数
        int row_num = 0;
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("会员积分明细表");
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        // 创建一个居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 单元格样式——单元格样式在.xls中最多定义4000种
        HSSFCellStyle cellStyle = cellStyle(wb);
        // 标题
 		HSSFRow row_header = sheet.createRow(row_num++);
        String[] headers = { "订单号", "交易单号", "订单类型", "订单状态", "支付方式", "订单总额", "实际支付", "创建时间", "商品编号", "商品名称", "商品规格",
				"商品数量","商品单价","收货人", "收货人电话", "收货地址" };
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row_header.createCell((short) i);
			// 设置cell编码解决中文高位字节截断
			cell.setCellValue(headers[i]);
			cell.setCellStyle(style);
		}
		// 3.单元格应用样式
		HSSFRow row_data;
		DateUtil date = DateUtil.getInstance();
		for (int i = 0; i < list.size(); i++) {
			OrderMain o = list.get(i);
			for (OrderDetail detail : o.getDetails()) {
				row_data = sheet.createRow(row_num++);
				// 订单号
				HSSFCell cell = row_data.createCell(0);
				cell.setCellValue(o.getOrderCode());
				cell.setCellStyle(cellStyle);
				// 交易单号
				cell = row_data.createCell(1);
				cell.setCellValue(o.getPayCode());
				cell.setCellStyle(cellStyle);
				// 订单类型
				cell = row_data.createCell(2);
				cell.setCellValue(o.getOrderTypeName());
				cell.setCellStyle(cellStyle);
				// 订单状态
				cell = row_data.createCell(3);
				cell.setCellValue(o.getOrderStatusName());
				cell.setCellStyle(cellStyle);
				// 支付方式
				cell = row_data.createCell(4);
				cell.setCellValue(o.getPayTypeName());
				cell.setCellStyle(cellStyle);
				// 订单总额
				cell = row_data.createCell(5);
				cell.setCellValue(o.getGoodsMoney().doubleValue());
				cell.setCellStyle(cellStyle);
				// 实际支付
				cell = row_data.createCell(6);
				cell.setCellValue(o.getRealMoney().doubleValue());
				cell.setCellStyle(cellStyle);
				// 创建时间
				cell = row_data.createCell(7);
				cell.setCellValue(date.toDateString(o.getCreateTime(), date.DEFAULT_DATETIME_PATTERN));
				cell.setCellStyle(cellStyle);
				
				// 商品信息
				cell = row_data.createCell(8);
				cell.setCellValue(detail.getProCode());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(9);
				cell.setCellValue(detail.getProName());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(10);
				cell.setCellValue(detail.getProSpecName());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(11);
				cell.setCellValue(detail.getQty());
				cell.setCellStyle(cellStyle);
				cell = row_data.createCell(12);
				cell.setCellValue(String.valueOf(detail.getPrice()));
				cell.setCellStyle(cellStyle);

				// 物流信息
				OrderExpDetail logisticsDetail = o.getExpDetail();
				if (logisticsDetail != null) {
					cell = row_data.createCell(13);
					cell.setCellValue(logisticsDetail.getReceiver());
					cell.setCellStyle(cellStyle);
					cell = row_data.createCell(14);
					cell.setCellValue(logisticsDetail.getPhone());
					cell.setCellStyle(cellStyle);
					cell = row_data.createCell(15);
					cell.setCellValue(logisticsDetail.getAddrPrefix() + " " + logisticsDetail.getAddressDetail());
					cell.setCellStyle(cellStyle);
				}
			}
			for (int k = 0; k <= 7; k++) {
				sheet.addMergedRegion(new CellRangeAddress(row_num - o.getDetails().size(), row_num - 1, k, k));
			}
			for (int k = 13; k <= 15; k++) {
				sheet.addMergedRegion(new CellRangeAddress(row_num - o.getDetails().size(), row_num - 1, k, k));
			}
			// 必须在单元格设值以后进行
            // 设置为根据内容自动调整列宽    
            sheet.autoSizeColumn(i);
		}
		return wb;
	}
	
	public static HSSFCellStyle cellStyle(HSSFWorkbook wb) {
		HSSFCellStyle cellStyle = wb.createCellStyle();// 新建单元格样式
//        cellStyle.setWrapText(true);//自动换行
		// 设置背景颜色
//		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 边框
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 
		// 居中
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中

		return cellStyle;
	}

}
